extends Node2D

## 流场寻路可视化测试脚本
## 左键设置起点，右键设置终点

@onready var tile_map_layer: TileMapLayer = %TileMapLayer
var path_finding:FlowFieldPathFinding
var path:Array[Vector2] = []
var time = 0
var start_pos
var end_pos

func _ready() -> void:
	for child in get_tree().current_scene.get_parent().get_children():
		if child is BaseSystem:
			await child.init_system()
			print("{0} 初始化完毕!".format([child.name]))
	GameEventBus.GameSystemInited.emit()
	## 显示第一个场景
	var fade_in_first_scene_options = GameSceneSystem.create_options(1, "fade")
	var first_scene_general_options = GameSceneSystem.create_general_options(Color(0, 0, 0), 1, false)
	GameSceneSystem.show_first_scene(fade_in_first_scene_options, first_scene_general_options)
	path_finding = FlowFieldPathFinding.new(tile_map_layer,1,FlowFieldPathFinding.DirectionType.Four)

func _process(delta: float) -> void:
	if Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT) and Time.get_ticks_msec() > time + 200:
		path.clear()
		time = Time.get_ticks_msec()
		end_pos = get_global_mouse_position()
		if not start_pos or not end_pos:
			return
		path_finding.set_target(end_pos)
		path = path_finding.get_path(start_pos)
		print(path)
		queue_redraw()
	if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) and Time.get_ticks_msec() > time + 200:
		path.clear()
		time = Time.get_ticks_msec()
		start_pos = get_global_mouse_position()
		if end_pos:
			path_finding.set_target(end_pos)
			path = path_finding.get_path(start_pos)
			print(path)
		queue_redraw()

func _draw() -> void:
	path_finding.draw_map(self)
	if not start_pos:
		return
	path_finding.draw_path(self,start_pos,path)
